package mygame.chameleonpoker;

import java.util.List;

import mygame.basicgameframework.Game;
import mygame.basicgameframework.Graphics;
import mygame.basicgameframework.Input.TouchEvent;
import mygame.basicgameframework.Screen;

public class MainMenuScreen extends Screen {
	
	boolean onlineButtonClicked = false;
	boolean singleButtonClicked = false;
	boolean settingsButtonClicked = false;
	boolean aboutButtonClicked = false;
	
	public MainMenuScreen(Game game) {
		super(game);
		Assets.menuMusic.setLooping(true);
		Assets.menuMusic.play();
	}
	
	private boolean inBounds(TouchEvent event, int x, int y, int width, int height) {
        if(event.x > x && event.x < x + width - 1 && 
           event.y > y && event.y < y + height - 1) 
            return true;
        else
            return false;
    }

	@Override
	public void update(float deltaTime) {
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		game.getInput().getKeyEvents();
		
		int len = touchEvents.size();
		for(int i = 0; i < len; i++) {
			
			TouchEvent event = touchEvents.get(i);
			
			if(event.type == TouchEvent.TOUCH_DOWN) {
				if(inBounds(event, 100, 240, 120, 40) ||
					inBounds(event, 100, 300, 120, 40)||
					inBounds(event, 100, 360, 120, 40)||
					inBounds(event, 100, 420, 120, 40)) 
					Assets.touch.play(1);
			}
			
			if(event.type == TouchEvent.TOUCH_DRAGGED) {
				if(inBounds(event, 100, 240, 120, 40)) {
					onlineButtonClicked = true;
				}
				else
					onlineButtonClicked = false;
				
				if(inBounds(event, 100, 300, 120, 40)) {
					singleButtonClicked = true;
				}
				else
					singleButtonClicked = false;
				
				if(inBounds(event, 100, 360, 120, 40)) {
					settingsButtonClicked = true;
				}
				else
					settingsButtonClicked = false;
				
				if(inBounds(event, 100, 420, 120, 40)) {
					aboutButtonClicked = true;
				}
				else
					aboutButtonClicked = false;
					
			}
			
			if(event.type == TouchEvent.TOUCH_UP) {
				if(inBounds(event, 100, 240, 120, 40)) {
					onlineButtonClicked = true;
					game.setScreen(new GameScreen(game));
				}
				else
					onlineButtonClicked = false;
				
				if(inBounds(event, 100, 300, 120, 40)) {
					singleButtonClicked = true;
				}
				else
					singleButtonClicked = false;
				
				if(inBounds(event, 100, 360, 120, 40)) {
					settingsButtonClicked = true;
				}
				else
					settingsButtonClicked = false;
				
				if(inBounds(event, 100, 420, 120, 40)) {
					aboutButtonClicked = true;
				}
				else
					aboutButtonClicked = false;
			}
		}

	}

	@Override
	public void present(float deltaTime) {
		Graphics g = game.getGraphics();
		g.drawPixelmap(Assets.mainMenu, 0, 0);
		if(onlineButtonClicked)
			g.drawPixelmap(Assets.buttonSingleClick, 100, 240);
		else
			g.drawPixelmap(Assets.buttonSingle, 100, 240);
		
		if(singleButtonClicked)
			g.drawPixelmap(Assets.buttonOnlineClick, 100, 300);
		else
			g.drawPixelmap(Assets.buttonOnline, 100, 300);
		
		if(settingsButtonClicked)
			g.drawPixelmap(Assets.buttonSettingsClick, 100, 360);
		else
			g.drawPixelmap(Assets.buttonSettings, 100, 360);
		
		if(aboutButtonClicked)
			g.drawPixelmap(Assets.buttonAboutClick, 100, 420);
		else
			g.drawPixelmap(Assets.buttonAbout, 100, 420);
	}

	@Override
	public void pause() {
		Assets.menuMusic.pause();
	}

	@Override
	public void resume() {
		if(!Assets.menuMusic.isPlaying())
			Assets.menuMusic.play();

	}

	@Override
	public void dispose() {
		
	}

}
